﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace MDigman.Camera.Media.Copier.WPF
{
    public interface IProgressContext
    {
        void UpdateStatus(string file, bool showVideo);
        void Finish();
    }

    /// <summary>
    /// Interaction logic for ProgressWindow.xaml
    /// </summary>
    public partial class ProgressWindow : Window, IProgressContext
    {
        private const int DELAY = 500;
        private double current = 1;

        public ProgressWindow(int max)
        {
            InitializeComponent();

            // Setup progress bar.
            Progress.Maximum = max;
        }

        public void UpdateStatus(string file, bool showVideo)
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Background,
                (SendOrPostCallback)delegate 
                {
                    int delay = 0;

                    if (file.ToUpper().EndsWith(".JPG"))
                    {
                        StatusText.SetValue(TextBlock.TextProperty, file);
                        aviViewer.Visibility = Visibility.Collapsed;

                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.DecodePixelWidth = 150;
                        bi.UriSource = new Uri(file);
                        bi.EndInit();

                        imgViewer.Visibility = Visibility.Visible;
                        imgViewer.Source = bi;
                        delay = DELAY;
                    }
                    else if (file.ToUpper().EndsWith(".AVI"))
                    {
                        StatusText.SetValue(TextBlock.TextProperty, file);
                        imgViewer.Visibility = Visibility.Collapsed;

                        if (showVideo)
                        {
                            aviViewer.Source = new Uri(file);
                            aviViewer.Play();

                            aviViewer.Visibility = Visibility.Visible;

                            delay = DELAY * 8;
                        }
                        else
                        {
                            // Don't play the video.
                            aviViewer.Visibility = Visibility.Hidden;
                            delay = DELAY;
                        }
                    }
                    Progress.SetValue(ProgressBar.ValueProperty, current++);
                    System.Threading.Thread.Sleep(delay);
                }, null);
        }

        public void Finish()    
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Background,
                (SendOrPostCallback)delegate
                {
                    System.Threading.Thread.Sleep(DELAY);
                    Close();
                }, null);
        }
    }
}
